package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.common.ResultMap.STATUS;
import com.financing.platform.dto.WxShopInfoDTO;
import com.financing.platform.service.IWxShopInfoService;
import com.financing.platform.util.DTOdateSet;

@Controller("wxShopInfoController")
@RequestMapping("/api/wxShopInfo")
public class WxShopInfoController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IWxShopInfoService wxShopInfoService;
	
	@RequestMapping("/listWxShopInfo.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, WxShopInfoDTO wxShopInfo,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query wxShopInfo list begin .......");
			
			wxShopInfo.setBegin(pager.getStartIndex());
			wxShopInfo.setPageSize(pager.getPageSize());
			
			int count = wxShopInfoService.queryListCount(wxShopInfo);
			
			pager.setTotalCount(count);
			
			List<WxShopInfoDTO> wxShopInfoList = null;
			
			if(count > 0){
				wxShopInfoList = wxShopInfoService.queryList(wxShopInfo);
			}else{
				wxShopInfoList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", wxShopInfoList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query wxShopInfo list end .......");
		}catch(Exception e){
			log.error("query wxShopInfo error!",e);
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addWxShopInfo.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, WxShopInfoDTO wxShopInfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add wxShopInfo  begin .......");
			
			wxShopInfo.setDeleteTag("1");
			
			DTOdateSet.setCreateInfo(wxShopInfo, request.getSession());
			DTOdateSet.setUpdateInfo(wxShopInfo, request.getSession());
			
			int id = wxShopInfoService.insert(wxShopInfo);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add wxShopInfo list end .......");
		}catch(Exception e){
			log.error("add wxShopInfo error!",e);
			result.setStatus(STATUS.FAILE);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailWxShopInfo.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, WxShopInfoDTO wxShopInfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail wxShopInfo  begin .......");
			
			Integer id = wxShopInfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			WxShopInfoDTO dto = wxShopInfoService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail wxShopInfo  end .......");
		}catch(Exception e){
			log.error("query detail wxShopInfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteWxShopInfo.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, WxShopInfoDTO wxShopInfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete wxShopInfo  begin .......");
			
			Integer id = wxShopInfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			wxShopInfoService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete wxShopInfo  end .......");
		}catch(Exception e){
			log.error("delete wxShopInfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateWxShopInfo.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, WxShopInfoDTO wxShopInfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update wxShopInfo  begin .......");
			
			Integer id = wxShopInfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
//			DTOdateSetUtil.setUpdateInfo(wxShopInfo);
			
			wxShopInfoService.update(wxShopInfo);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update wxShopInfo list end .......");
		}catch(Exception e){
			log.error("update wxShopInfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceWxShopInfo.shtml")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, WxShopInfoDTO wxShopInfo){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce wxShopInfo  begin .......");
			
			Integer id = wxShopInfo.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
//			DTOdateSetUtil.setUpdateInfo(wxShopInfo);
			
			wxShopInfoService.updateForce(wxShopInfo);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce wxShopInfo list end .......");
		}catch(Exception e){
			log.error("updateForce wxShopInfo error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
